package com.muyu.danmo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoCommunity;
import com.muyu.danmo.service.IDanmoCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 社区信息Controller
 *
 * @author : kunzong
 * @date : 2024-11-19
 */
@RestController
@RequestMapping("/danmoCommunity")
public class DanmoCommunityController extends BaseController {
    @Autowired
    private IDanmoCommunityService danmoCommunityService;


    /**
     * 查询所有帖子
     */
    @PostMapping("/list")
    public Result list(@RequestBody DanmoCommunity danmoCommunity) {
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        List<DanmoCommunity> list = danmoCommunityService.selectDanmoCommunityList(danmoCommunity);

        if (list.size() == 0) {
            return Result.success(list, "暂无信息");
        }
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }

    /**
     * 导出社区表列表
     */
    @Log(title = "社区表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoCommunity danmoCommunity) {
        List<DanmoCommunity> list = danmoCommunityService.selectDanmoCommunityList(danmoCommunity);
        ExcelUtil<DanmoCommunity> util = new ExcelUtil<>(DanmoCommunity.class);
        util.exportExcel(response, list, "社区表数据");
    }

    /**
     * 帖子详情
     */
    @GetMapping(value = "details/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        DanmoCommunity danmoCommunity = danmoCommunityService.selectDanmoCommunityById(id);
        if (danmoCommunity != null) {
            return Result.success(danmoCommunity);
        } else {
            return Result.error("帖子被隐藏/不存在");
        }
    }

    /**
     * 新增帖子
     */
    @Log(title = "社区表", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public Result add(@RequestBody DanmoCommunity danmoCommunity) {
        return danmoCommunityService.insertDanmoCommunity(danmoCommunity);
    }

    /**
     * 修改帖子
     */
    @Log(title = "社区表", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public Result edit(@RequestBody DanmoCommunity danmoCommunity) {
        String token = SecurityUtils.getToken();
        System.out.println("token" + token);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoCommunityService.updateDanmoCommunity(danmoCommunity);
    }

    /**
     * 删除帖子
     */
    @Log(title = "社区表", businessType = BusinessType.DELETE)
    @DeleteMapping("del/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoCommunityService.deleteDanmoCommunityByIds(ids);
        return Result.success(ids, "删除成功");
    }


    /**
     * 隐藏帖子(可多选)
     */
    @Log(title = "社区表", businessType = BusinessType.DELETE)
    @GetMapping("hidePosts/{ids}")
    public Result hidePosts(@PathVariable Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoCommunityService.hidePostsDanmoCommunityByIds(ids);
    }

    /**
     * 取消隐藏帖子(可多选)
     */
    @Log(title = "社区表", businessType = BusinessType.DELETE)
    @GetMapping("cancelHiddenPosts/{ids}")
    public Result cancelHiddenPosts(@PathVariable Long[] ids) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoCommunityService.cancelHidePostsDanmoCommunityByIds(ids);
    }

    /**
     * 查询关注用户的帖子
     */
    @PostMapping("/attentionList")
    public Result attentionList(@RequestBody DanmoCommunity danmoCommunity) {
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        List<DanmoCommunity> list = danmoCommunityService.selectDanmoCommunityAttentionList(danmoCommunity);
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }

    /**
     * 我的社区
     */
    @PostMapping("/myCommunity")
    public Result myCommunity(@RequestBody DanmoCommunity danmoCommunity) {
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoCommunity.setAuthorId(loginUser.getUserid());
        List<DanmoCommunity> list = danmoCommunityService.selectDanmoCommunityMyCommunityList(danmoCommunity);
        if (list.size() == 0) {
            return Result.error("暂无帖子");
        }
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }

    /**
     * 查询他人社区
     */
    @PostMapping("/otherCommunity")
    public Result otherCommunity(@RequestBody DanmoCommunity danmoCommunity) {
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        List<DanmoCommunity> list = danmoCommunityService.selectDanmoCommunityList(danmoCommunity);
        if (list.size() == 0) {
            return Result.error("暂无帖子");
        }
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }

    /**
     * 查询热门帖子(排序)
     */
    @PostMapping("/popular")
    public Result popular(@RequestBody DanmoCommunity danmoCommunity) {
        PageHelper.startPage(danmoCommunity.getPageNum(), danmoCommunity.getPageSize());
        List<DanmoCommunity> list = danmoCommunityService.selectDanmoCommunityPopularList(danmoCommunity);
        if (list.size() == 0) {
            return Result.error("暂无帖子");
        }
        PageInfo<DanmoCommunity> danmoCommunityPageInfo = new PageInfo<>(list);
        return Result.success(danmoCommunityPageInfo);
    }
}